package com.cq.digitalemployee.shiro.redis;

import org.apache.shiro.cache.CacheException;

import java.io.*;

/**
 * 对象序列化操作类
 *
 * @author zzz
 */
public class SerializeUtil {

    public static byte[] serialize(Object object) {
        ByteArrayOutputStream byteArrayOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(object);
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            throw new CacheException(e);
        }
    }

    public static Object unSerialize(byte[] bytes) {
        ByteArrayInputStream byteArrayInputStream = null;
        ObjectInputStream objectInputStream = null;
        try {
            byteArrayInputStream = new ByteArrayInputStream(bytes);
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            Object object = objectInputStream.readObject();
            return object;
        } catch (IOException ioe) {
            throw new CacheException(ioe);
        } catch (ClassNotFoundException cne) {
            throw new CacheException(cne);
        }
    }
}
